import poser, string, os
os.chdir(os.path.dirname(poser.AppLocation()))

class PzrRect:
	def __init__(self, id=1, left=43, top=96, right=53, bottom=106):
		self.id = id
		self.top = top
		self.left = left
		self.right = right
		self.bottom = bottom

class PzrText:
	def __init__(self, color=-16777216, size=14, align=0, font=0, italic=0, antialiased=1, offsetX=0, offsetY=0):
		self.color = color
		self.size = size
		self.align = align
		self.font = font
		self.italic = italic
		self.antialiased = antialiased
		self.offsetX = offsetX
		self.offsetY = offsetY

class PzrPillow:
	def __init__(self, left=-1, middle=-1, right=-1, lbtn=26020, mbtn=26019, rbtn=26018):
		self.left = left
		self.middle = middle
		self.right = right
		self.lbtn = lbtn
		self.mbtn = mbtn
		self.rbtn = rbtn

def FindHex(i):
	h = hex(i)[2:]
	if len(h) < 2: h = "0" + h
	return h

def PzrColor(R,G,B):
	if R > 255: raise "Red value exceeded 255."
	if G > 255: raise "Green value exceeded 255."
	if B > 255: raise "Blue value exceeded 255."
	rgb = "0x00" + FindHex(R) + FindHex(G) + FindHex(B)
	return string.atoi(rgb,16)

class PzrXMLTag:
	def __init__(self, type="Widget", opener="<", closer="/>", spacer=" "):
		self.type = type
		self.closer = closer
		self.spacer = spacer
		self.string = opener + type + spacer
	def addValue(self, arg, value):
		if value == "none": return None
		if type(arg) != type("string"):
			arg = repr(arg)
		if type(value) != type("string"):
			value = repr(value)
		self.string = self.string + arg + '="' + value + '"' + self.spacer
	def close(self):
		self.string = self.string + self.closer
		return self.string
	def get(self):
		return self.string

class PzrWidgetList:
	def __init__(self, file, PSDFiles=["./runtime/ui/26000_utility.psd"], useBorders=1, margin_left=20, margin_top=30, margin_right=20, margin_bottom=30):
		self.file = file
		self.path = self.formatPath(self.file)
		self.xml = """<?xml version="1.0"?>"""
		self.addXML( self.addTag("WidgetList", {"XmlFile":self.path}, closer=">"), indent=0)
		for PSD in PSDFiles:
			self.addXML( self.addTag("PSDFile", {"name":PSD}), indent=1 )
		self.addXML( PzrBackground("Background").place(-10,-10), indent=1)
		if useBorders:
			self.addXML( PzrTopLeftBorder(width=margin_left, height=margin_top).place(0,0), indent=1)
			self.addXML( PzrTopCenterBorder(width=margin_left, height=margin_top).place(0,0), indent=1)
			self.addXML( PzrTopRightBorder(width=margin_right, height=margin_top).place(0,0), indent=1)
			self.addXML( PzrLeftBorder(width=margin_left, height=margin_top).place(0,0), indent=1)
			self.addXML( PzrRightBorder(width=margin_right, height=margin_top).place(0,0), indent=1)
			self.addXML( PzrBottomLeftBorder(width=margin_left, height=margin_bottom).place(0,0), indent=1)
			self.addXML( PzrBottomCenterBorder(width=margin_left, height=margin_bottom).place(0,0), indent=1)
			self.addXML( PzrBottomRightBorder(width=margin_right, height=margin_bottom).place(0,0), indent=1)
	def addXML(self, tag, indent=0):
		txt = self.xml + "\n\n"
		for i in range(indent):
			txt = txt + "\t"
		txt = txt + tag
		self.xml = txt
	def addTag(self, type, values, opener="<", closer="/>"):
		tag = PzrXMLTag(type=type, opener=opener, closer=closer)
		for key in values.keys():
			tag.addValue(key, values[key])
		tag.close()
		return tag.get()
	def close(self):
		self.addXML( self.addTag("StringResIDs", {"major":0}), indent=1)
		self.addXML( self.addTag("WidgetList", {}, opener="</",closer=">"), indent=0)
		self.writeXML(self.file, self.xml)
		return self.xml
	def formatPath(self, path):
		path = string.join(string.split(path,"\\"),"/")
		path = string.join(string.split(path,":"),"/")
		if path[0] == "/": path = path[1:]
		return "./" + path
	def writeXML(self, path, data):
		fi = open(path,'w')
		fi.write(data)
		fi.close()
		return path

class PzrWidget:
	command = "none"
	groupID = "none"
	pictRes = "none"
	hiliteRes = "none"
	behaviorProcID = "none"
	preliteAction = "none"
	bb = "none"
	persistBetweenRooms = "none"
	rect = "none"
	useRectClick = "none"
	pin = "none"
	color = "none"
	fadeMax = "none"
	tile = "none"
	isStringID = "none"
	text = "none"
	padding = "none"
	pillow = "none"
	widgetText = "none"
	useAlpha = "none"
	widgetProcID = "none"
	def configure(self, value=-1, callback=-1, title=-1):
		if value != -1: self.buttonValue = value
		if callback != -1: self.callback = callback
		if title != -1: self.title = title
	def AddButtonCallback(self, dialog, callback):
		dialog.AddButtonCallback(self.widgetName, callback=callback)
	def SetButtonValue(self, dialog, value):
		dialog.SetButtonValue(self.widgetName, value=value)
	def SetText(self, dialog, message):
		dialog.SetText(self.widgetName, text=message)
	def SetDialog(self, dialog):
		dialog.SetText(self.widgetName, text=self.title)
		dialog.SetButtonValue(self.widgetName, value=self.buttonValue)
		if self.callback: dialog.AddButtonCallback(self.widgetName, callback=self.callback)
	def __init__(self, widgetName, callback=None, height=0, width=0, buttonValue=0, command=command, groupID=groupID, pictRes=pictRes, hiliteRes=hiliteRes, behaviorProcID=behaviorProcID, preliteAction=preliteAction, bb=bb, persistBetweenRooms=persistBetweenRooms, rect=rect, useRectClick=useRectClick, pin=pin, color=color, fadeMax=fadeMax, tile=tile, isStringID=isStringID, text=text, padding=padding, pillow=pillow, widgetText=widgetText, useAlpha=useAlpha, widgetProcID=widgetProcID):
		self.title = widgetName
		self.callback = callback
		self.buttonValue = buttonValue
		self.widgetName = widgetName
		self.command = command
		self.groupID = groupID
		self.pictRes = pictRes
		self.hiliteRes = hiliteRes
		self.behaviorProcID = behaviorProcID
		self.preliteAction = preliteAction
		self.bb = bb
		self.persistBetweenRooms = persistBetweenRooms
		self.rect = rect
		self.useRectClick = useRectClick
		self.pin = pin
		self.color = color
		self.fadeMax = fadeMax
		self.tile = tile
		self.isStringID = isStringID
		self.text = text
		self.padding = padding
		self.pillow = pillow
		self.widgetText = widgetText
		self.useAlpha = useAlpha
		self.widgetProcID = widgetProcID
		if height and width:
			self.bb = PzrRect(left=0,top=0,bottom=height,right=width)
			self.rect = PzrRect(left=0,top=0,bottom=height,right=width)
	def place(self, x, y):
		self.xml = PzrXMLTag(type="Widget")
		self.xml.addValue("name", self.widgetName)
		self.xml.addValue("command", self.command)
		self.xml.addValue("groupID", self.groupID)
		self.xml.addValue("pictRes", self.pictRes)
		self.xml.addValue("hiliteRes", self.hiliteRes)
		self.xml.addValue("behaviorProcID", self.behaviorProcID)
		self.xml.addValue("widgetProcID", self.widgetProcID)
		self.xml.addValue("preliteAction", self.preliteAction)
		if self.bb != "none":
			self.xml.addValue("left",self.bb.left + x)
			self.xml.addValue("top", self.bb.top + y)
			self.xml.addValue("right", self.bb.right + x)
			self.xml.addValue("bottom", self.bb.bottom + y)
		self.xml.addValue("persistBetweenRooms", self.persistBetweenRooms)
		self.xml.addValue("useRectClick", self.useRectClick)
		if self.rect != "none":
			self.xml.addValue("rect.left", self.rect.left + x)
			self.xml.addValue("rect.top", self.rect.top + y)
			self.xml.addValue("rect.right", self.rect.right + x)
			self.xml.addValue("rect.bottom", self.rect.bottom + y)
		if self.pin != "none":
			self.xml.addValue("pinID", self.pin.id)
			self.xml.addValue("pinTop", self.pin.top)
			self.xml.addValue("pinLeft", self.pin.left)
			self.xml.addValue("pinBottom", self.pin.bottom)
			self.xml.addValue("pinRight", self.pin.right)
		self.xml.addValue("color", self.color)
		self.xml.addValue("fadeMax", self.fadeMax)
		self.xml.addValue("tile", self.tile)
		self.xml.addValue("isStringID", self.isStringID)
		if self.text != "none":
			self.xml.addValue("textColor", self.text.color)
			self.xml.addValue("textSize", self.text.size)
			self.xml.addValue("textAlign", self.text.align)
			self.xml.addValue("textFont", self.text.font)
			self.xml.addValue("textItalic", self.text.italic)
			self.xml.addValue("textAntialiased", self.text.antialiased)
			self.xml.addValue("textOffsetX", self.text.offsetX)
			self.xml.addValue("textOffsetY", self.text.offsetY)
		self.xml.addValue("padding", self.padding)
		if self.pillow != "none":
			self.xml.addValue("leftPillowID", self.pillow.left)
			self.xml.addValue("middlePillowID", self.pillow.middle)
			self.xml.addValue("rightPillowID", self.pillow.right)
			self.xml.addValue("leftButtonID", self.pillow.lbtn)
			self.xml.addValue("middleButtonID", self.pillow.mbtn)
			self.xml.addValue("rightButtonID", self.pillow.rbtn)
		self.xml.addValue("widgetText", self.widgetText)
		self.xml.addValue("useAlpha", self.useAlpha)
		self.xml.close()
		return self.xml.get()

def PzrBackground(widgetName, command=0, pictRes=26076, behaviorProcID=4, bb=PzrRect(left=0, top=0, right=26, bottom=26), persistBetweenRooms=1, rect=PzrRect(left=0, top=0, right=480, bottom=400), pin=PzrRect(id=1, left=1, top=3, right=2, bottom=4), color=-1, fadeMax=0, tile=1):
	return PzrWidget(widgetName, command=command, pictRes=pictRes, behaviorProcID=behaviorProcID, bb=bb, persistBetweenRooms=persistBetweenRooms, rect=rect, pin=pin, color=color, fadeMax=fadeMax, tile=tile)

def PzrBorder(widgetName="topLeftDlgBorder",  command=0,  pictRes=26084,  behaviorProcID=4,  pin=PzrRect(id=1,left=1,top=3,right=1,bottom=3),  bb=PzrRect(left=0,top=0,bottom=30,right=20),  persistBetweenRooms=1, rect=PzrRect(left=0,top=0,right=20,bottom=30),  color=-1,  fadeMax=0, tile="none"):
	return PzrWidget(widgetName,  command=command,  pictRes=pictRes,  behaviorProcID=behaviorProcID,  pin=pin,  bb=bb,  persistBetweenRooms=persistBetweenRooms, rect=rect,  color=color,  fadeMax=fadeMax, tile=tile)

def PzrTopLeftBorder(width=20, height=30):
	return PzrBorder(widgetName="topLeftDlgBorder",  pictRes=26084,  pin=PzrRect(id=1,left=1,top=3,right=1,bottom=3),  bb=PzrRect(left=0,top=0,bottom=height,right=width), rect=PzrRect(left=0,top=0,right=20,bottom=30))

def PzrTopCenterBorder(height=30, width=20):
	return PzrBorder(widgetName="topCenterDlgBorder",  pictRes=26083,  pin=PzrRect(id=1,left=1,top=3,right=2,bottom=3),  bb=PzrRect(left=width,top=0,bottom=height-3,right=width+40), rect=PzrRect(left=20,top=0,right=560,bottom=27), tile=1)

def PzrTopRightBorder(width=20, height=30):
	return PzrBorder(widgetName="topRightDlgBorder",  pictRes=26082,  pin=PzrRect(id=1,left=2,top=3,right=2,bottom=3),  bb=PzrRect(left=-1*width,top=0,bottom=height,right=0), rect=PzrRect(left=-20,top=0,right=0,bottom=30))

def PzrLeftBorder(width=20, height=30):
	return PzrBorder(widgetName="leftDlgBorder",  pictRes=26081,  pin=PzrRect(id=1,left=1,top=3,right=1,bottom=4),  bb=PzrRect(left=0,top=height,bottom=-1*height,right=width-6), rect=PzrRect(left=0,top=80,right=14,bottom=420), tile=1)

def PzrRightBorder(width=20, height=30):
	return PzrBorder(widgetName="rightDlgBorder",  pictRes=26080,  pin=PzrRect(id=1,left=2,top=3,right=2,bottom=4),  bb=PzrRect(left=-1*width+6,top=height,bottom=-1*height,right=0), rect=PzrRect(left=486,top=80,right=500,bottom=420), tile=1)

def PzrBottomLeftBorder(width=20, height=30):
	return PzrBorder(widgetName="bottomLeftDlgBorder",  pictRes=26079,  pin=PzrRect(id=1,left=1,top=4,right=1,bottom=4),  bb=PzrRect(left=0,top=-1*height-10,bottom=0,right=width), rect=PzrRect(left=0,top=410,right=20,bottom=450))

def PzrBottomCenterBorder(width=20, height=30):
	return PzrBorder(widgetName="bottomMiddleDlgBorder",  pictRes=26078,  pin=PzrRect(id=1,left=1,top=4,right=2,bottom=4),  bb=PzrRect(left=width,top=-1*height-5,bottom=0,right=-1*width), rect=PzrRect(left=20,top=415,right=480,bottom=450), tile=1)

def PzrBottomRightBorder(width=-20,height=30):
	return PzrBorder(widgetName="bottomRightDlgBorder",  pictRes=26077,  pin=PzrRect(id=1,left=2,top=4,right=2,bottom=4),  bb=PzrRect(left=-1*width,top=-1*height-10,bottom=0,right=0), rect=PzrRect(left=480,top=410,right=500,bottom=450))

def PzrRadiobutton(widgetName, command=10002, groupID=23, pictRes=26072, hiliteRes=26071, behaviorProcID=3, preliteAction=1, bb=PzrRect(left=0,top=0,right=10,bottom=10), rect=PzrRect(left=0,top=0,right=10,bottom=10), useRectClick=1, pin=PzrRect(id=1,left=1,top=3,right=1,bottom=3), color=-1, fadeMax=0):
	return PzrWidget(widgetName, command=command, groupID=groupID, pictRes=pictRes, hiliteRes=hiliteRes, behaviorProcID=behaviorProcID, preliteAction=preliteAction, bb=bb, rect=rect, useRectClick=useRectClick, pin=pin, color=color, fadeMax=fadeMax)

def PzrCheckbutton(widgetName, command=10002, groupID=23, pictRes=26105, hiliteRes=26104, behaviorProcID=3, preliteAction=1, bb=PzrRect(left=0,top=0,right=17,bottom=18), rect=PzrRect(left=0,top=0,right=17,bottom=18), useRectClick=1, pin=PzrRect(id=1,left=1,top=3,right=1,bottom=3), color=-1, fadeMax=0):
	return PzrWidget(widgetName, command=command, groupID=groupID, pictRes=pictRes, hiliteRes=hiliteRes, behaviorProcID=behaviorProcID, preliteAction=preliteAction, bb=bb, rect=rect, useRectClick=useRectClick, pin=pin, color=color, fadeMax=fadeMax)

def PzrMessage(widgetName, command=0, groupID=23, widgetProcID=3, height=0, width=0, widgetText="0:2", isStringID=1, text=PzrText(), preliteAction=6, pin=PzrRect(id=1,top=3,left=1,bottom=3,right=1), rect=PzrRect(left=0,top=0,right=116,bottom=21), bb=PzrRect(left=0,top=0,right=116,bottom=21), color=-1, fadeMax=0):
	return PzrWidget(widgetName, command=command, groupID=groupID, widgetProcID=widgetProcID, height=height, width=width, widgetText=widgetText, isStringID=isStringID, text=text, preliteAction=preliteAction, pin=pin, rect=rect, bb=bb, color=color, fadeMax=fadeMax)

def PzrButtonWidget(widgetName, command=10001, groupID=4, pictRes=26019, widgetProcID=23, height=0, width=0, padding=12, pillow=PzrPillow(), widgetText="0:2", isStringID=1, text=PzrText(color=-16777216,size=16,align=0), behaviorProcID=1, useAlpha=0, preliteAction=1, pin=PzrRect(id=1,top=3,left=1,bottom=3,right=1), bb=PzrRect(left=0,top=0,right=60,bottom=20), persistBetweenRooms=1, rect=PzrRect(left=0,top=0,right=60,bottom=20), color=-16777216, fadeMax=0):
	return PzrWidget(widgetName, command=command, groupID=4, pictRes=pictRes, widgetProcID=widgetProcID, height=height, width=width, padding=padding, pillow=pillow, widgetText=widgetText, isStringID=isStringID, text=text, behaviorProcID=behaviorProcID, useAlpha=useAlpha, preliteAction=preliteAction, pin=pin, bb=bb, persistBetweenRooms=persistBetweenRooms, rect=rect, color=color, fadeMax=fadeMax)

def PzrImage(widgetName, command=0, pictRes=26084, behaviorProcID=4, pin=PzrRect(id=1,left=1,top=3,right=1,bottom=3), height=0, width=0, bb=PzrRect(left=0,top=0,bottom=64,right=64), persistBetweenRooms=1, rect=PzrRect(left=0,top=0,right=64,bottom=64),  color=-1,  fadeMax=0, tile="none"):
	return PzrWidget(widgetName, command=command, pictRes=pictRes, behaviorProcID=behaviorProcID, pin=pin, bb=bb, height=height, width=width, persistBetweenRooms=persistBetweenRooms, rect=bb,  color=color,  fadeMax=fadeMax, tile=tile)

def PzrImageButton(widgetName, callback=None, command=10002, groupID=23, pictRes=26105, hiliteRes=26104, width=0, height=0, behaviorProcID=1, preliteAction=1, useRectClick=1, pin=PzrRect(id=1,left=1,top=3,right=1,bottom=3), color=-1, fadeMax=0):
	return PzrWidget(widgetName, callback=callback, command=command, groupID=groupID, pictRes=pictRes, hiliteRes=hiliteRes, width=width, height=height, behaviorProcID=behaviorProcID, preliteAction=preliteAction, pin=pin, color=color, fadeMax=fadeMax )

class PzrRadio:
	def __init__(self, widgetName, callback=None, command=10002, groupID=23, width=126, height=21):
		self.widgetName = widgetName
		self.title = widgetName
		self.command = command
		self.groupID = groupID
		self.btn = PzrRadiobutton(widgetName, command=command, groupID=groupID)
		self.msg = PzrMessage(widgetName + "Message", command=command, groupID=groupID, bb=PzrRect(left=6,top=-12,right=width-10,bottom=height-10))
		self.width = width
		self.height = height
		self.callback = callback
	def place(self, x, y):
		return "\t" + self.btn.place(x, y) + "\n\t" + self.msg.place(x+10,y+10)
	def SetText(self, dialog, message):
		self.msg.SetText(dialog, message)
	def SetDialog(self, dialog):
		self.SetText(dialog, self.widgetName)
		if self.callback:
			dialog.AddButtonCallback(self.widgetName, callback=self.callback)
			dialog.AddButtonCallback(self.msg.widgetName, callback=self.callback)

class PzrCheck:
	def __init__(self, widgetName, initOn=0, callback=None, command=10002, groupID=23, width=126, height=21):
		self.widgetName = widgetName
		self.callback  = callback
		self.title = widgetName
		self.command = command
		self.groupID = groupID
		self.btn = PzrCheckbutton(widgetName, command=command, groupID=groupID)
		self.msg = PzrMessage(widgetName + "Message", command=command, groupID=groupID, bb=PzrRect(left=8,top=-8,right=width-10,bottom=height-10))
		self.width = width
		self.height = height
		self.var = initOn
	def place(self, x, y):
		return "\t" + self.btn.place(x, y) + "\n\t" + self.msg.place(x+10,y+10)
	def get(self):
		return self.var
	def toggle(self, dialog):
		if not self.var: self.select(dialog)
		else: self.deselect(dialog)
	def select(self, dialog):
		self.var = 1
		dialog.SetButtonValue(name=self.widgetName, value=1)
	def deselect(self, dialog):
		self.var = 0
		dialog.SetButtonValue(name=self.widgetName, value=0)
	def SetText(self, dialog, message):
		self.msg.SetText(dialog, message)
	def SetDialog(self, dialog):
		self.SetText(dialog, self.widgetName)
		dialog.SetButtonValue(self.widgetName, self.var)
		if self.callback:
			dialog.AddButtonCallback(self.widgetName, callback=self.callback)
		else:
			dialog.AddButtonCallback(self.widgetName, callback=lambda v, s=self, d=dialog: s.toggle(d))

class PzrRadioPane:
	def __init__(self, groupID=23, initCommand=10002, itemsPerCol=13):
		self.buttons = []
		self.var = 0
		self.initCommand = initCommand
		self.curCommand = initCommand
		self.groupID = groupID
		self.itemsPerCol = itemsPerCol
		self.lookup_table = {}
		self.padding = {}
	def AddRadiobutton(self, widgetName):
		command = self.curCommand
		self.curCommand = command + 1
		self.buttons.append( PzrRadio(widgetName, command=command, groupID=self.groupID) )
		self.lookup_table[widgetName] = len(self.buttons)-1
		self.var = self.buttons[0].widgetName
	def SetText(self, dialog):
		for button in self.buttons:
			button.SetText(dialog, button.widgetName)
	def AddPaddingAfter(self, widgetName, padX, padY):
		self.padding[widgetName] = (padX, padY)
	def place(self, x, y):
		self.xml = ""
		curx = x
		cury = y
		i = 1
		for button in self.buttons:
			self.xml = self.xml + "\n" + button.place(curx, cury)
			if self.padding.has_key(button.widgetName):
				x = x + self.padding[button.widgetName][0]
				y = y + self.padding[button.widgetName][1]
			curx = x + int(i / self.itemsPerCol) * button.width
			cury = y + int(i % self.itemsPerCol) * button.height
			i = i + 1
		return self.xml
	def get(self):
		return self.var
	def set(self, value):
		self.var = value
		for button in self.buttons:
			if button.widgetName == value: continue
			self.dlg.SetButtonValue(name=button.widgetName, value=0)
		self.dlg.SetButtonValue(name=value, value=1)
		self.onClick(value)
	def onClick(self, v):
		pass
	def setOnClick(self, callback):
		self.onClick = lambda v: callback(v)
	def SetValues(self, dialog):
		for button in self.buttons:
			dialog.AddButtonCallback(name=button.widgetName, callback=lambda v, s=self, wN=button.widgetName: s.set(wN))
			dialog.SetButtonValue(name=button.widgetName, value=0)
		dialog.SetButtonValue(self.buttons[0].widgetName, value=1)
	def SetDialog(self, dialog):
		self.dlg = dialog
		self.SetText(dialog)
		self.SetValues(dialog)
		self.set( self.var )

class PzrCheckPane:
	def __init__(self, groupID=23, initCommand=10002, itemsPerCol=13, valueMap=None):
		self.buttons = []
		self.selected = []
		self.initCommand = initCommand
		self.curCommand = initCommand
		self.groupID = groupID
		self.itemsPerCol = itemsPerCol
		self.valueMap = valueMap
		self.lookup_table = {}
		self.padding = {}
	def AddCheckbutton(self, widgetName, width=126, height=21):
		command = self.curCommand
		self.curCommand = command + 1
		return self.AddButton( PzrCheck(widgetName, command=command, groupID=self.groupID, width=width, height=height) )
	def AddButton(self, button):
		self.buttons.append(button)
		self.lookup_table[button.widgetName] = len(self.buttons)-1
		return button
	def SetText(self, dialog):
		for button in self.buttons:
			button.SetText(dialog, button.widgetName)
	def AddPaddingAfter(self, widgetName, padX, padY):
		self.padding[widgetName] = (padX, padY)
	def place(self, x, y):
		self.xml = ""
		curx = x
		cury = y
		i = 1
		for button in self.buttons:
			self.xml = self.xml + "\n" + button.place(curx, cury)
			if self.padding.has_key(button.widgetName):
				x = x + self.padding[button.widgetName][0]
				y = y + self.padding[button.widgetName][1]
			curx = x + int(i / self.itemsPerCol) * button.width
			cury = y + int(i % self.itemsPerCol) * button.height
			i = i + 1
		return self.xml
	def get(self):
		return self.selected
	def select(self, dialog, widgetName):
		if not widgetName in self.selected:
			self.selected.append(widgetName)
		dialog.SetButtonValue(name=widgetName, value=1)
	def deselect(self, dialog, widgetName):
		if widgetName in self.selected:
			self.selected.remove(widgetName)
		dialog.SetButtonValue(name=widgetName, value=0)
	def set(self, dialog, value):
		if value in self.selected:
			self.selected.remove(value)
			dialog.SetButtonValue(name=value, value=0)
		else:
			self.selected.append(value)
			dialog.SetButtonValue(name=value, value=1)
	def SetValues(self, dialog):
		if not self.valueMap: self.valueMap = [0] * len(self.buttons)
		self.dialog = dialog
		i = 0
		for button in self.buttons:
			dialog.AddButtonCallback(name=button.widgetName, callback=lambda v, s=self, d=dialog, n=button.widgetName: s.set(d,n))
			if self.valueMap[i]: self.select(dialog, button.widgetName)
			else: self.deselect(dialog, button.widgetName)
			i = i + 1
	def SetDialog(self, dialog):
		self.SetText(dialog)
		self.SetValues(dialog)

def PzrOkButton():
	return PzrWidget(widgetName="okBtn",  command=7001,  groupID=4,  pictRes=26016,  behaviorProcID=1,  preliteAction=1,  pin=PzrRect(id=1,top=3,left=1,bottom=3,right=1),  bb=PzrRect(left=0,top=0,bottom=19,right=58),  persistBetweenRooms=1,  rect=PzrRect(left=0,top=0,right=58,bottom=19),  color=-1,  fadeMax=0)

def PzrCancelButton():
	return PzrWidget(widgetName="cancelBtn",  command=7002,  groupID=4,  pictRes=26017,  behaviorProcID=1,  preliteAction=1,  pin=PzrRect(id=1,top=3,left=1,bottom=3,right=1),  bb=PzrRect(left=0,top=0,bottom=19,right=58),  persistBetweenRooms=1,  rect=PzrRect(left=0,top=0,right=58,bottom=19),  color=-1,  fadeMax=0)

class PzrButton:
	def __init__(self, widgetName, callback=None, command=10002, groupID=4):
		self.btn = PzrButtonWidget(widgetName, command=command, groupID=groupID)
		self.widgetName = widgetName
		self.callback = callback
	def SetDialog(self,dialog,function=None):
		self.SetText(dialog)
		if function: self.callback = function
		self.AddButtonCallback(dialog,self.callback)
	def AddButtonCallback(self,dialog,function):
		if function: dialog.AddButtonCallback(self.widgetName, function)
	def SetText(self, dialog):
		dialog.SetText(self.widgetName, text=self.widgetName)
	def place(self, x, y):
		return self.btn.place(x, y)

class PzrValueButton(PzrButton):
	defaultText = "Load"
	defaultValue = None
	defaultMessage = "Pick value."
	defaultOptions = []
	null = "!NULL"
	def __init__(self, widgetName, command=10002, groupID=4, text=None, value="!NULL", message=None, options=None):
		self.btn = PzrButton(widgetName, command=command, groupID=groupID)
		self.widgetName = widgetName
		if value != self.null:
			self.default = value
		else:
			self.default = self.defaultValue
		if message:
			self.message = message
		else:
			self.message = self.defaultMessage
		if options:
			self.options = options
		else:
			self.options = self.defaultOptions
		if text:
			self.title = self.FormatText(text)
		else:
			self.title = self.defaultText
		self.value = self.default
	def get(self):
		return self.FormatValue(self.value)
	def set(self, text, value):
		self.ChangeText(text)
		self.ChangeValue(value)
	def OnClick(self, event=None):
		newValue = self.AskValue()
		if newValue != self.null:
			self.value = newValue
			self.ChangeText( self.FormatText(newValue) )
	def AskValue(self):
		return self.null
	def FormatText(self, text):
		return text
	def FormatValue(self, value):
		return value
	def ChangeText(self, text):
		text = self.FormatText(text)
		self.title = text
		try: self.SetText(self.dlg)
		except: pass
	def ChangeValue(self, value):
		self.value = self.FormatValue(value)
	def SetDialog(self, dialog, function=None):
		self.dlg = dialog
		self.SetText(dialog)
		self.AddButtonCallback(dialog, self.OnClick)
	def SetText(self, dialog):
		dialog.SetText(self.widgetName, text=self.title)

class PzrDirButton(PzrValueButton):
	defaultText = "Load"
	defaultValue = os.path.dirname(poser.AppLocation())
	def AskValue(self):
		folder = AskDirectory(dir=self.default, title="Select a directory...")
		if folder: return folder
		else: return self.null

class PzrFileButton(PzrValueButton):
	defaultText = "Load"
	defaultValue = os.path.dirname(poser.AppLocation())
	defaultMessage = "Open..."
	defaultExtensions = (".pz3", ".pzz")
	def AskValue(self):
		file = AskOpen(extensions=self.defaultExtensions, dir=self.default, title=self.message)
		if file:
			return file
		else:
			return self.null

class PzrMenuButton(PzrValueButton):
	defaultText = "Choose"
	defaultValue = None
	defaultMessage = "Select an item."
	defaultOptions = []
	def GetOptions(self):
		return self.options
	def AskValue(self):
		item = AskMenu(message=self.message, options=self.GetOptions())
		if item: return item
		else: return self.null

class PzrFloatButton(PzrValueButton):
	defaultText = "0.0"
	defaultValue = 0
	defaultMessage = "Enter a number."
	def AskValue(self, event=None):
		asked = poser.DialogSimple.AskFloat(self.message)
		if asked: return asked
		else: return self.null
	def FormatText(self, text):
		return repr(text)
	def FormatValue(self, value):
		return float(value)

class PzrIntButton(PzrValueButton):
	defaultText = "0.0"
	defaultValue = 0
	defaultMessage = "Enter an integer."
	def AskValue(self, event=None):
		return poser.DialogSimple.AskInt(self.message)
	def FormatText(self, text):
		return repr(text)
	def FormatValue(self, value):
		return int(value)

class PzrPickImageButton(PzrValueButton):
	defaultText = "None"
	defaultValue = None
	def AskValue(self, event=None):
		return poser.DialogSimple.PickImage(self.message)
	def FormatText(self, text):
		if text:
			return os.path.basename(text)
		else:
			return "None"
	def FormatValue(self, value):
		return value

class TextButton(PzrButton):
	def __init__(self, widgetName, text="Enter", format="float", command=10002, groupID=4):
		self.btn = PzrButton(widgetName, command=command, groupID=groupID)
		self.widgetName = widgetName
		self.format = format
		self.title = self.FormatText(text)
		self.value = self.Convert(text)
	def get(self):
		return self.value
	def Convert(self, val):
		if self.format == "float":
			return float(val)
		elif self.format == "int":
			return int(val)
		else:
			return val
	def AskValue(self, event=None):
		newValue = AskText(message="")
		if newValue:
			try:
				self.value = self.Convert(newValue)
				self.ChangeText(newValue)
			except:
				ErrorBox("Invalid data: %s" % newValue)
	def FormatText(self, text):
		return text
	def ChangeText(self, text):
		text = self.FormatText(text)
		self.title = text
		self.SetText(self.dlg)
	def SetDialog(self, dialog, function=None):
		self.dlg = dialog
		self.SetText(dialog)
		self.AddButtonCallback(dialog, self.AskValue)
	def SetText(self, dialog):
		dialog.SetText(self.widgetName, text=self.title)

def MessageBox(message="Text goes here"):
	poser.DialogSimple.MessageBox(message)

def ErrorBox(message="Error!"):
	MessageBox(message)

def AskText(message="\nEnter the name for\nthe new group."):
	txt = poser.DialogTextEntry(message=message)
	if txt.Show():
		return txt.Text()
	else:
		return 0

def AskDirectory(dir=os.path.dirname(poser.AppLocation()),title="Open...",parent=None):
	askdir = poser.DialogDirChooser(parentDialog=parent, message=title, startDir=dir)
	if askdir.Show():
		folder = askdir.Path()
		os.chdir(os.path.dirname(poser.AppLocation()))
		if not os.path.isdir(folder):
			return None
		else:
			return folder
	else:
		return None

def AskOpen(extensions=(".fx6"),dir=os.path.dirname(poser.AppLocation()),title="Open...",parent=None):
	askfile = poser.DialogFileChooser(type=poser.kDialogFileChooserOpen, parentDialog=parent, message=title, startDir=dir)
	askfile.Show()
	file = askfile.Path()
	os.chdir(os.path.dirname(poser.AppLocation()))
	if not os.path.isfile(file):
		return None
	else:
		good = 0
		for ext in extensions:
			if not string.lower(file[-1 * len(ext):]) == string.lower(ext):
				continue
			else:
				good = 1
		if not good:
			error_string = """Bad file type. Must be """
			for ext in extensions:
				if (ext == extensions[-1]) and (len(extensions) > 1):
					thread = " or " + ext + "."
				elif len(extensions) > 1:
					thread = ext + ", "
				else:
					thread = ext + "."
				error_string = error_string +  thread
			ErrorBox(message=error_string)
			return AskOpen(extensions=extensions, dir=dir, title=title)
	return file

def AskSaveAs(extensions=(".txt"),dir=os.path.dirname(poser.AppLocation()),title="Save as...",parent=None):
	askfile = poser.DialogFileChooser(type=poser.kDialogFileChooserSave, parentDialog=parent, message=title, startDir=dir)
	askfile.Show()
	file = askfile.Path()
	if os.path.isdir(file):
		file = None
	else:
		good = 0
		for ext in extensions:
			if not string.lower(file[-1 * len(ext):]) == ext:
				continue
			else:
				good = 1
		if not good:
			file = file + extensions[0]
	os.chdir(os.path.dirname(poser.AppLocation()))
	return file

def AskMenu(title="Title", message="Select item", options=[]):
	if options == []:
		return None
	return poser.DialogSimple.AskMenu(title, message, options)

class PzrDialog:
	def __init__(self, title="", message="", width=480, height=320, xmlpath="tempDlg.xml", borders=1, palettes=[]):
		self.widgets = []
		self.xmlpath = xmlpath
		self.title = title
		self.message = message
		self.width = width
		self.height = height
		if type(palettes) != type(list): palettes = [palettes]
		self.xmlManager = PzrWidgetList( self.xmlpath, PSDFiles=(["./runtime/ui/26000_utility.psd"] + palettes), useBorders=borders )
		self.showing = 0
	def add(self, widget, x, y):
		self.widgets.append((widget, x, y))
	def AddButtonCallback(self, name=None, callback=None):
		self.dlg.AddButtonCallback(name=name, callback=callback)
	def SetButtonValue(self, name=None, value=0):
		self.dlg.SetButtonValue(name=name, value=value)
	def SetText(self, name=None, text=""):
		self.dlg.SetText(name=name, text=text)
	def Show(self):
		self.showing = 1
		for widget, x, y in self.widgets:
			self.xmlManager.addXML( widget.place(x,y) )
		self.xmlManager.close()
		self.dlg = poser.Dialog( self.xmlpath, title=self.title, message=self.message, width=self.width, height=self.height)
		for widget, x, y in self.widgets:
			widget.SetDialog( self.dlg )
			#self.dlg.SetText(widget.widgetName, text=widget.title)
		self.onShow()
		return self.dlg.Show()
	def Text(self):
		return self.dlg.Text()
	def get(self):
		return None
	def onShow(self):
		return None

def ShowAd():
	xmlMan = PzrWidgetList("tempDlg.xml", PSDFiles=["./runtime/ui/26000_utility.psd", "./Runtime/Python/PoseWorks/ShaderSpider/Data/12000.psd"], useBorders=0)

	img = PzrWidget("ss6Bg", command=0, pictRes=12047, behaviorProcID=4, bb=PzrRect(left=0, top=0, right=600, bottom=374), rect=PzrRect(left=0, top=0, right=600, bottom=374))
	xbtn = PzrWidget("xBtn", command=7001, pictRes=12048, bb=PzrRect(left=0,top=0,right=26,bottom=26), rect=PzrRect(left=0,top=0,right=26,bottom=26))

	xmlMan.addXML( img.place(0,0) )
	xmlMan.addXML( xbtn.place(562, 9) )

	xmlMan.close()
	ad = poser.Dialog("tempDlg.xml", width=600, height=374)
	ad.Show()

if __name__ == '__main__':
	pass
pass
